{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_secret = \"5dGgwXlJoT5nP2cY2yYq27F6YiGTNusW\"\n",
    "api_key = \"MpXN4nWMRaeYA61UWnhRrfrftONIxdvG\"   # Replace with a valid Subscription Key here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet Create"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. FaceSet Create\n",
    "import requests,json\n",
    "\n",
    "display_name = \"人脸集合1019\"# 自定义人脸集合的名字\n",
    "outer_id = \"20201019\" # 自定义标识\n",
    "user_data = \"test\" # 自定义用户信息\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\" # 调用URL\n",
    "payload = {\n",
    "    # 请求参数\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "    'user_data':user_data\n",
    "}\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(CreateFace_Url, params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa7c1d608b11b1b5c02c568409e474bb',\n",
       " 'time_used': 168,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603521459,ab2df884-b2eb-4ea4-8494-a36f0bbb54f0',\n",
       " 'outer_id': '20201019',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet GetDetail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取人脸集合信息\n",
    "GetDetail_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(GetDetail_Url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa7c1d608b11b1b5c02c568409e474bb',\n",
       " 'tags': '',\n",
       " 'time_used': 92,\n",
       " 'user_data': 'test',\n",
       " 'display_name': '人脸集合1019',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603521502,6efda21b-6af3-430e-86fb-9ddef3cf73be',\n",
       " 'outer_id': '20201019'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet AddFace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 增加人脸信息\n",
    "AddFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'fa7c1d608b11b1b5c02c568409e474bb',\n",
    "    'face_tokens':'9fa4dcfdc04d479957394929b6cb1272',# \n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(AddFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa7c1d608b11b1b5c02c568409e474bb',\n",
       " 'time_used': 685,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1603522910,928cfbeb-768d-4ae4-b5a8-3c048a0c5b98',\n",
       " 'outer_id': '20201019',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet RemoveFace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 移除人脸信息\n",
    "RemoveFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'1654abc733c5bc62b1513afb97a9045a',\n",
    "    'face_tokens':'9fa4dcfdc04d479957394929b6cb1272',# 从delect中获取的\n",
    "}\n",
    "r = requests.post(RemoveFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '1654abc733c5bc62b1513afb97a9045a',\n",
       " 'face_removed': 0,\n",
       " 'time_used': 194,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603522940,f1866b2d-bb88-48c3-b805-20fb51dcacbc',\n",
       " 'outer_id': '00001',\n",
       " 'failure_detail': [{'reason': 'FACE_NOT_IN_FACESET',\n",
       "   'face_token': '9fa4dcfdc04d479957394929b6cb1272'}]}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet Update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 更新人脸信息\n",
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'1654abc733c5bc62b1513afb97a9045a',\n",
    "    'user_data':\"test\",\n",
    "}\n",
    "r = requests.post(Update_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '1654abc733c5bc62b1513afb97a9045a',\n",
       " 'request_id': '1603522948,24c31725-7ece-489d-8257-2ee7e8e42bc5',\n",
       " 'time_used': 70,\n",
       " 'outer_id': '00001'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "pakho01 = \"https://gitee.com/fengyanfen-new/api/raw/master/picture/pakho.jpg\"\n",
    "wanglingkan = \"https://gitee.com/fengyanfen-new/api/raw/master/picture/wlk.jpg\"\n",
    "huangzongze = \"https://gitee.com/fengyanfen-new/api/raw/master/picture/800.jpg\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare Face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 98,\n",
       "    'top': 87,\n",
       "    'left': 119,\n",
       "    'height': 98},\n",
       "   'face_token': '31b471debe35f896a8a2ce0b5ad02d38'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 115,\n",
       "    'top': 109,\n",
       "    'left': 286,\n",
       "    'height': 115},\n",
       "   'face_token': '3a003bd345d7921f5986e1c5e3ee1118'}],\n",
       " 'time_used': 3019,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 56.26,\n",
       " 'image_id2': 'eXcYSRC3BRMaqkaKUO7utA==',\n",
       " 'image_id1': 'xsdnEEq59Jag0g1G/TJl/g==',\n",
       " 'request_id': '1603522951,f752c4ed-96e2-495a-b7e3-d1e25e42e72f'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 人脸相似度对比\n",
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'image_url1':wanglingkan,\n",
    "    'image_url2':huangzongze\n",
    "}\n",
    "r = requests.post(Compare_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
